package com.kust.msmService.controller;

import com.kust.commomUtils.JwtUtils;
import com.kust.commomUtils.Result;
import com.kust.msmService.service.MsmService;
import com.kust.msmService.utils.RandomUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 马强
 * @Description
 * @create 2022-06-30 10:41
 */
@RestController
@RequestMapping("/eduMsm/msm")
public class MsmController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping(value = "/send/{phone}")
    public Result code(@PathVariable String phone) {
        //从redis获取验证码，如果获取到就直接返回
        String code = redisTemplate.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return Result.ok();
        }
        //如果redis读取不到，说明失效了 使用腾讯云重新发送
        //生成随机值 传递腾讯云进行发送
        code = RandomUtil.getFourBitRandom();
        System.out.println("验证码为："+code);
        //调用service发送短信
        boolean isSend = msmService.send(phone, code);
        if(isSend) {
            redisTemplate.opsForValue().set(phone, code,10, TimeUnit.MINUTES);
            return Result.ok();
        } else {
            return Result.error().message("发送短信失败");
        }
    }


}
